<%@page import="com.google.appengine.repackaged.com.google.common.base.StringUtil"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.PreparedQuery" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>

<script>
	function checkNameAvailable(_idPlayerName)
	{
		var name=document.getElementById(_idPlayerName).value;
		//window.open("http://3dividedby3.appspot.com/chess.jsp?name="+name, "_self");
		window.open("http://localhost:8888/chess.jsp?name="+name, "_self");
	}
</script>

</head>
<body>
<%
String currentPlayerName=request.getParameter("name");

if ( StringUtil.isEmptyOrWhitespace(currentPlayerName) )
{
	return;
}

String playerListNames="playerListNames";
Key genericKey = KeyFactory.createKey("Starting", playerListNames);

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

Query q = new Query("namesInPlay");
PreparedQuery pq = datastore.prepare(q);

String warning=null;

out.println("<select>");
for ( Entity playerEntity: pq.asIterable() )
{
	String playerName=(String)playerEntity.getProperty("name");
	out.println("<option value=\""+playerName+"\">"+playerName+"</option>");
	
	if ( playerEntity.getProperty("name").equals(currentPlayerName) )
	{
		warning="Name aready exists";
	}
	else
	{	
		Entity playerNameToBeAdded=new Entity("namesInPlay", genericKey);
		playerName=(String)playerNameToBeAdded.getProperty("name");
		out.println("<option value=\""+playerName+"\">"+playerName+"</option>");
		playerNameToBeAdded.setProperty("name", currentPlayerName);
		playerNameToBeAdded.setProperty( "creationTime", Long.toString(System.currentTimeMillis()) );
	}
}
out.println("</select>");
%>




Player name: <input type="text" name="playerName" id="idPlayerName">
<button type="button" onclick="checkNameAvailable('idPlayerName');">Choose name!</button>

<p>Hello ${fn:escapeXml(user.nickname)}! </p>
<%= "hello".toString() %>

</body>
</html>